<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ersv
 * Date: 1/11/13
 * Time: 11:54 AM
 * To change this template use File | Settings | File Templates.
 */
/**
 *  WRITE
 */
function write_ini_file($file, array $options){
    $tmp = '';
    foreach($options as $section => $values){
        $tmp .= "[$section]\n";
        foreach($values as $key => $val){
            if(is_array($val)){
                foreach($val as $k =>$v){
                    $tmp .= "{$key}[$k] = \"$v\"\n";
                }
            }
            else
                $tmp .= "$key = \"$val\"\n";
        }
        $tmp .= "\n";
    }
    file_put_contents($file, $tmp);
    unset($tmp);
}
if(file_exists(ROOT_PATH."/config/database.ini"))
{
    $mess = "";
    if(isset($_POST["update"]))
    {
        $array=array("Database" => array(
            "host" => $_POST["host"],
            "database" => $_POST["database"],
            "user_name" => $_POST["user_name"],
            "password" => $_POST["password"],
        ));
        write_ini_file(ROOT_PATH."/config/database.ini",$array);
        $mess = "<span style='color: #006400;'>Update Successful!</span>";
    }
    $config=parse_ini_file(ROOT_PATH."/config/database.ini");
    $host=$config["host"];
    $dbname=$config["database"];
    $id=$config["user_name"];
    $pass=$config["password"];
?>
    <form action="GeneralCode.php?action=config_database.php" method="post">
    <table class="table">
        <tr><th colspan="2">Config Database</th></tr>
        <tr>

            <th>
            </th>
            <td>
                <?php echo $mess; ?>
            </td>
        </tr>
        <tr>
            <th>
                Host
            </th>
            <td>
                <input type="text" name="host" value="<?php echo $host; ?>">
            </td>
        </tr>
        <tr>
            <th>
                Database
            </th>
            <td>
                <input type="text" name="database" value="<?php echo $dbname; ?>">
            </td>
        </tr>
        <tr>
            <th>
                User Name
            </th>
            <td>
                <input type="text" name="user_name" value="<?php echo $id; ?>">
            </td>
        </tr>
        <tr>
            <th>
                Password
            </th>
            <td>
                <input type="text" name="password" value="<?php echo $pass; ?>">
            </td>
        </tr>
        <tr>
            <th></th>
            <td><input type="submit" value="Update" name="update"></td>
        </tr>
    </table>
    </form>
<?php
}
else
{
    echo "<span style='color: #a52a2a;'>error: file database.ini not exists.</span>";
}